//
//  LoginViewController.m
//  糗事百科
//
//  Created by 奚超明 on 16/5/28.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "LoginViewController.h"
#import "UserModel.h"
#import "SettingUserNameViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    // 标题
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.title = @"登录 / 注册";
    
    // 左标题
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, 0, 50, 30);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:kOrangerColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelLogin) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    // 登录按钮圆角
    self.loginButton.layer.cornerRadius = 5;
    self.loginButton.layer.masksToBounds = YES;
}

#pragma mark - 微信登录
- (IBAction)loginByWeiXin:(UIButton *)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             // 授权成功，存储用户模型
             [self userModelWithUid:user.uid token:user.credential.token nickname:user.nickname icon:user.icon platformType:SSDKPlatformTypeWechat];
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}

#pragma mark - QQ登录
- (IBAction)loginByQQ:(UIButton *)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             // 授权成功，存储用户模型
             [self userModelWithUid:user.uid token:user.credential.token nickname:user.nickname icon:user.icon platformType:SSDKPlatformTypeQQ];
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}

#pragma mark - 微博登录
- (IBAction)loginByWeiBo:(UIButton *)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             // 授权成功，存储用户模型
             [self userModelWithUid:user.uid token:user.credential.token nickname:user.nickname icon:user.icon platformType:SSDKPlatformTypeSinaWeibo];
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
             NSLog(@"icon=%@", user.icon);
         }
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}

#pragma mark - 登录成功存储模型
- (void)userModelWithUid:(NSString *)uid token:(NSString *)token nickname:(NSString *)nickname icon:(NSString *)icon platformType:(SSDKPlatformType)platformType
{
    UserModel *user = [[UserModel alloc] init];
    user.token = token;
    user.uid = uid;
    user.nickname = nickname;
    user.icon = icon;
    user.platformType = platformType;
    
    // 判断是否需要跳转昵称界面
    
    
    // 跳转修改昵称界面
    SettingUserNameViewController *settingUserNameVC = [[SettingUserNameViewController alloc] init];
    settingUserNameVC.userModel = user;
    [self.navigationController pushViewController:settingUserNameVC animated:YES];
}

#pragma mark - 糗百登录
- (IBAction)loginByQiuBai:(UIButton *)sender {
    
}

#pragma mark - 退出登录
- (void)cancelLogin
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

@end
